﻿// <copyright file="ControlHelpers.cs" company="Auros Ltd.">
//     Copyright (c) Auros Ltd. All rights reserved.
// </copyright>

namespace Auros.Categorisation
{
    using System.Web.UI;

    /// <summary>
    /// Class containing a number of helper methods for managing and manipulating controls.
    /// </summary>
    internal static class ControlHelpers
    {
        /// <summary>
        /// Recursively finds all controls that are of a specified type starting within the current control.
        /// </summary>
        /// <typeparam name="T">Type of control to find.</typeparam>
        /// <param name="container">The container control.</param>
        /// <returns>Enumerable collection of controls.</returns>
        internal static System.Collections.Generic.IEnumerable<T> GetControlsByTypeRecursive<T>(Control container)
        {
            foreach (var item in container.Controls)
            {
                if (item is T)
                    yield return (T)item;

                if ((item as Control).HasControls())
                {
                    foreach (var childControl in GetControlsByTypeRecursive<T>(item as Control))
                    {
                        yield return childControl;
                    }
                }
            }
        }
    }
}